#include "comLogManager.h"
#include "ttcodeStateChart.h"

#include "templateLuaMgr.h"

#include <boost/assign/list_of.hpp>
#include <boost/regex.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/filesystem.hpp>


std::map<std::string,std::string> globalTTEnv;

std::map<std::string,std::string> globalFileType = boost::assign::map_list_of \
(".h","head") \
("h","head") \
(".hpp","head") \
("hpp","head") \
("head","head") \
(".c","cstd") \
("c","cstd") \
(".cpp","cpp") \
("cpp","cpp") \
(".cxx","cpp") \
("cxx","cpp") \
(".cx","cpp") \
("cx","cpp") \
(".cp","cpp") \
("cp","cpp") \
(".cc","cpp") \
("cc","cpp") \
(".cl","cl") \
("cl","cl") \
;


MainFrameMgr *globalFrame = NULL;
MainFrameAui *globalAui = NULL;
ProjectNaviMgr *globalNavi = NULL;
TextEditMgr *globalTextEdit = NULL;
InfoSpaceMgr *globalInfo = NULL;


ProjectDocMgr *globalDocMgr = NULL;
IconImageMgr *globalIiMgr = NULL;
TemplateMgr *globalTempMgr = NULL;
InfoCollectMgr* globalCollectMgr = NULL;


ttcodeMainCtrlStateChart globalStateChart;



void stateAppStart::onInitWidgets( const eventInitWidgets & )
{
	globalDocMgr = new ProjectDocMgr;
	globalIiMgr = new IconImageMgr;
	globalTempMgr = new TemplateMgr;
	globalCollectMgr = new InfoCollectMgr;

	globalNavi = new ProjectNaviMgr(globalFrame);
	globalTextEdit = new TextEditMgr(globalFrame);
	globalInfo = new InfoSpaceMgr(globalFrame);
	globalAui = new MainFrameAui();

	templateLuaMgr luaMgr(globalTTEnv["tt_home"]);
	std::string script;
	script += "globalTTHome=\"";
	script += globalTTEnv["tt_home"];
	script += "\"\n";
	script += "globalLuaPath=\"";
	script += globalTTEnv["tt_home"] + "/tt-app/luaScript/";
	script += "\"\n";
	luaMgr.callLuaString( script );
	ttcode_log << "script=<"<< script << ">" << endl;
	luaMgr.callLuaApp( globalTTEnv["tt_home"] + "/tt-app/luaScript/checkEnv.lua" );
}

void stateAppStart::onLoadProject( const eventLoadProject & )
{
	globalDocMgr->loadProjectFile(globalTTEnv["doc_name"]);
	globalNavi->reloadProject();
	globalStateChart.process_event( evendLoadFinnish() );
}


void stateEmpty::onNewProject( const eventNewProject & evt)
{
	globalTTEnv["doc_name"] = evt.name;
	globalTTEnv["doc_home"] = evt.home;
	globalDocMgr->loadProjectFile(globalTTEnv["doc_name"]);
	globalNavi->reloadProject();
	globalStateChart.process_event( evendLoadFinnish() );
}


void stateProjectOpen::onCloseProject( const eventCloseProject & evt)
{
	globalStateChart.process_event( eventCloseFinnish() );
}
void stateProjectOpen::onNewProject( const eventNewProject & evt)
{
	//
	globalStateChart.process_event( eventCloseProject() );
	//
	globalTTEnv["doc_name"] = evt.name;
	globalTTEnv["doc_home"] = evt.home;
	globalDocMgr->loadProjectFile(globalTTEnv["doc_name"]);
	globalNavi->reloadProject();
	globalStateChart.process_event( evendLoadFinnish() );
}


void stateProjectOpen::onBuild( const eventBuild & evt)
{
	string config = "debug";
	unsigned short port = globalCollectMgr->startListen();

	ostringstream outs;
	outs << evt.param;
	outs << " ";
	outs << "--config=" << config << " ";
//	outs << "--arch=native ";
	outs << "--port=";
	outs << port;
	ttcode_log << "outs.str()=" << outs.str() << endl;
#if WIN32
	globalCollectMgr->daemonApplicationNative(globalTTEnv["tt_home"]+"/tt-app/bin/ttbuild.exe ",outs.str());
#else
	globalCollectMgr->daemonApplicationNative(globalTTEnv["tt_home"]+"/tt-app/bin/ttbuild ",outs.str());
#endif
}
void stateProjectOpen::onClean( const eventClean & evt)
{
	string config = "debug";
	unsigned short port = globalCollectMgr->startListen();

	ostringstream outs;
	outs << "--config=" << config << " ";
	outs << "--clean ";
	outs << "--port=";
	outs << port;
	ttcode_log << "outs.str()=" << outs.str() << endl;
#if WIN32
	globalCollectMgr->daemonApplicationNative(globalTTEnv["tt_home"]+"/tt-app/bin/ttbuild.exe ",outs.str());
#else
	globalCollectMgr->daemonApplicationNative(globalTTEnv["tt_home"]+"/tt-app/bin/ttbuild ",outs.str());
#endif
}


void stateProjectOpen::onFillReportDirect( const eventFillReportDirect & evt)
{
	TiXmlElement* elem = globalDocMgr->getFileByPath(evt.filepath);
	ttcode_log << "evt.filepath=<"<< evt.filepath << ">" << endl;
	ttcode_log << "evt.pos=<"<< evt.pos << ">" << endl;
	ttcode_log << "evt.content=<"<< evt.content << ">" << endl;
	ttcode_log << "evt.tips=<"<< evt.tips << ">" << endl;

	if( NULL == elem )
	{
		boost::filesystem::path filePath(evt.filepath);
		if(boost::filesystem::exists(filePath))
		{
			globalTextEdit->tryOpenExternalFile(evt.filepath);
		}
		else
		{
			return;
		}
	}
	else
	{
		globalTextEdit->tryOpenFile(elem);
	}

	int line,col;
	line = col = 0;
	boost::smatch what;
	if( boost::regex_search(evt.pos,what, boost::regex(":(\\d+):(\\d+):")) )
	{
		string lineStr(what[1]);
		string colStr(what[2]);
		ttcode_log << "lineStr=<"<< lineStr << ">" << endl;
		ttcode_log << "colStr=<"<< colStr << ">" << endl;
		try
		{
			line = boost::lexical_cast<int>(lineStr) ;
			col = boost::lexical_cast<int>(colStr) ;
			globalTextEdit->tryGotoPos(line,col,evt.content);
		}
		catch(boost::bad_lexical_cast & e)
		{
		}
	}
}

void stateProjectOpen::onCloseAll( const eventCloseAll & evt)
{
	delete globalDocMgr;
	delete globalIiMgr;
	delete globalTempMgr;
	delete globalCollectMgr;
}


